function register() {
    particlesJS('particles-js', {
        "particles": {
            "number": {
                "value": 70,
                "density": {
                    "enable": true,
                    "value_area": 800
                }
            },
            "color": {
                "value": "#00f7ff"
            },
            "shape": {
                "type": "circle",
                "stroke": {
                    "width": 0,
                    "color": "#000000"
                }
            },
            "opacity": {
                "value": 0.5,
                "random": true,
                "anim": {
                    "enable": true,
                    "speed": 1,
                    "opacity_min": 0.1,
                    "sync": false
                }
            },
            "size": {
                "value": 3,
                "random": true,
                "anim": {
                    "enable": true,
                    "speed": 2,
                    "size_min": 0.1,
                    "sync": false
                }
            },
            "line_linked": {
                "enable": true,
                "distance": 150,
                "color": "#00f7ff",
                "opacity": 0.4,
                "width": 1
            },
            "move": {
                "enable": true,
                "speed": 2,
                "direction": "none",
                "random": true,
                "straight": false,
                "out_mode": "out",
                "bounce": false
            }
        },
        "interactivity": {
            "detect_on": "canvas",
            "events": {
                "onhover": {
                    "enable": true,
                    "mode": "grab"
                },
                "resize": true
            },
            "modes": {
                "grab": {
                    "distance": 140,
                    "line_linked": {
                        "opacity": 1
                    }
                }
            }
        },
        "retina_detect": true
    });
}
register()

// 获取验证码按钮点击事件
document.getElementById('getCodeBtn').addEventListener('click', function() {
    const email = document.getElementById('email').value;
    const emailError = document.getElementById('emailError');
    const btn = this;
    const successMessage = document.getElementById('successMessage');
    const codeError = document.getElementById('codeError');

    // 重置错误信息
    emailError.textContent = '';
    codeError.textContent = '';
    successMessage.textContent = '';

    // 验证邮箱格式
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
        emailError.textContent = '请输入有效的邮箱地址';
        return;
    }

    // 禁用按钮
    btn.disabled = true;
    successMessage.textContent = '验证码发送中...';

    // 发送获取验证码请求
    fetch('/api/send_verification_code', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ email })
    })
    .then(response => response.json())
    .then(data => {
        if (data.message === '验证码已发送' || data.message === '验证码已发送（开发环境）') {
            successMessage.textContent = data.message;

            // 如果是开发环境，显示验证码
            if (data.debug_code) {
                console.log('验证码（开发环境）:', data.debug_code);
                successMessage.textContent += `，验证码: ${data.debug_code}`;
            }

            // 60秒倒计时
            let seconds = 60;
            const timer = setInterval(() => {
                btn.textContent = `重新获取(${seconds}s)`;
                seconds--;

                if (seconds < 0) {
                    clearInterval(timer);
                    btn.disabled = false;
                    btn.textContent = '获取验证码';
                    successMessage.textContent = '';
                }
            }, 1000);
        } else {
            // 清除"验证码发送中..."提示
            successMessage.textContent = '';
            codeError.textContent = data.message || '发送验证码失败';
            btn.disabled = false;
            btn.textContent = '获取验证码';
        }
    })
    .catch(error => {
        console.error('错误:', error);
        // 清除"验证码发送中..."提示
        successMessage.textContent = '';
        codeError.textContent = '服务器错误';
        btn.disabled = false;
        btn.textContent = '获取验证码';
    });
});

// 注册表单提交事件
document.getElementById('registerForm').addEventListener('submit', function(e) {
    e.preventDefault();
    const loader = document.getElementById('loader');
    const generalError = document.getElementById('generalError');

    // 重置所有错误信息
    document.querySelectorAll('.error').forEach(element => {
        element.textContent = '';
    });
    document.getElementById('successMessage').textContent = '';
    generalError.textContent = '';

    loader.style.display = 'block';

    // 获取表单数据
    const username = document.getElementById('username').value;
    const email = document.getElementById('email').value;
    const password = document.getElementById('password').value;
    const confirmPassword = document.getElementById('confirmPassword').value;
    const verificationCode = document.getElementById('verificationCode').value;

    // 验证用户名
    if (username.length < 3) {
        loader.style.display = 'none';
        document.getElementById('usernameError').textContent = '用户名至少需要3个字符';
        return;
    }

    // 验证邮箱格式
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
        loader.style.display = 'none';
        document.getElementById('emailError').textContent = '请输入有效的邮箱地址';
        return;
    }

    // 验证验证码
    if (!verificationCode) {
        loader.style.display = 'none';
        document.getElementById('codeError').textContent = '请输入验证码';
        return;
    }

    // 验证密码
    if (password.length < 6) {
        loader.style.display = 'none';
        document.getElementById('passwordError').textContent = '密码至少需要6个字符';
        return;
    }

    // 验证密码确认
    if (password !== confirmPassword) {
        loader.style.display = 'none';
        document.getElementById('confirmPasswordError').textContent = '两次输入的密码不一致';
        return;
    }

    // 发送注册请求
    fetch('/api/register', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            username,
            email,
            password,
            verificationCode
        })
    })
    .then(response => response.json())
    .then(data => {
        loader.style.display = 'none';

        if (data.message === '注册成功') {
            // 显示成功消息并跳转
            const successMessage = document.getElementById('successMessage');
            successMessage.textContent = '注册成功！请前往邮箱激活账号';
            successMessage.style.color = 'var(--success-color)';

            // 1秒后跳转到登录页面
            setTimeout(() => {
                window.location.href = '/login';
            }, 1000);
        } else {
            generalError.textContent = data.message;
        }
    })
    .catch(error => {
        loader.style.display = 'none';
        console.error('错误:', error);
        generalError.textContent = '服务器错误';
    });
});